package com.luzhikun.path.com.luzhikun.myExamSystem.controller;

import com.luzhikun.path.com.luzhikun.myExamSystem.dao.UserDao;
import com.luzhikun.path.com.luzhikun.myExamSystem.model.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * ClassName: UserAddServlet
 * Description:
 * date: 2021/4/22 18:03
 *
 * @author luzhikun
 * @since JDK 1.8
 */
public class UserAddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userName, passWord, sex, email;
        //调用请求对象【读取】请求头中的参数信息
        userName = req.getParameter("userName");
        passWord = req.getParameter("passWord");
        sex = req.getParameter("sex");
        email = req.getParameter("email");
        //创建判断成功与否结果
        boolean isFlag;
        //创建用户窗口对象
        UserDao userDao = new UserDao();
        //创建浏览器响应对象
        PrintWriter out = null;
        //【调用userDao】将用户信息填充到INSERT命令并借助JDBC规范发送给数据库服务器
        User user = new User(userName, passWord, sex, email);
        //添加用户
        isFlag = userDao.addUser(user);
        resp.setContentType("text/html;charset=UTF-8");
        //[调用响应对象]将处理结果以二进制形式写到响应体中
        out = resp.getWriter();
        if (isFlag) {
            out.println("用户注册成功");
        } else {
            out.println("用户注册失败");
        }
    }
    //Tomcat负责销毁【请求对象】和【响应对象】
    //Tomcat负责将Http响应协议包推送给客户端浏览器
    //浏览器根据响应头content-type指定的编译器对响应体中的二进制数据进行编辑
    //浏览器将编辑后的结果展示给用户【结束】

}
